package top.healthylife.controller;/*
 *@Author GaoZeXi
 *@Created time 2020/4/15 6:06
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import top.healthylife.constant.MessageConstant;
import top.healthylife.constant.RedisConstant;
import top.healthylife.constant.RedisMessageConstant;
import top.healthylife.entity.Result;
import top.healthylife.utils.SMSUtils;
import top.healthylife.utils.ValidateCodeUtils;

/**
 * 验证码
 * @author GaoZeXi
 * @date 2020-4-15 06:11:51
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //生成4为验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        System.out.println("【用户手机号】"+telephone+"\t【手机验证码】:"+code);
        //调用阿里云短信验证码api
        try {
             SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            //将验证码存入redis缓存
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,
                    60*30,code.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //验证码发送失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //生成6为验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        System.out.println("【用户手机号】"+telephone+"\t【登陆验证码】:"+code);

        //调用阿里云短信验证码api
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            //将验证码存入redis缓存
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,
                    60*30,code.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //验证码发送失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }
}
